package com.jf.spring.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-10-29
 * Time:19:53
 */
@Service
public class GoodService {
    @Autowired
    private GoodDAO goodDAO;
    @Transactional
    public void buyGoods() {
        goodDAO.buyGood(1);
        goodDAO.buyGood(2,2);
    }
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void viewGoods() {
        int stock = goodDAO.viewGoodStock(1);
        System.out.println("stock = " + stock);
        stock = goodDAO.viewGoodStock(1);
        System.out.println("stock = " + stock);
    }
    @Transactional(timeout = 1)
    public void buyGoods(int amount) {
        int stock = goodDAO.viewGoodStock(1);
        System.out.println("stock = " + stock);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        goodDAO.buyGood2(1,amount);
    }
}
